home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -in_the_mag- / reader_requests / mui38dev-storm / examples / brush2c.c < prev    next >
C/C++ Source or Header  |  1999-09-13  |  4KB  |  156 lines

  1. /*
  2. ** This little program can be used to convert an ILBM brush
  3. ** to something that MUI's Bodychunk class can understand.
  4. **
  5. ** Perfect for about logos and stuff like that...
  6. **
  7. ** Have fun,
  8. ** Stefan.
  9. */
  10.  
  11. #include "exec/types.h"
  12. #include "exec/memory.h"
  13. #include "libraries/dos.h"
  14. #include "libraries/iffparse.h"
  15. #include "datatypes/pictureclass.h"
  16. #include "graphics/gfx.h"
  17. #include "proto/dos.h"
  18. #include "proto/iffparse.h"
  19. #include "stdlib.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22. #include "ctype.h"
  23.  
  24.  
  25. #define to32(c) (((c)<<24)|((c)<<16)|((c)<<8)|(c))
  26.  
  27. #define ID_ANIM MAKE_ID('A','N','I','M')
  28. #define ID_ANHD MAKE_ID('A','N','H','D')
  29. #define ID_DLTA MAKE_ID('D','L','T','A')
  30.  
  31. /*FR* StormC does only have strict ANSI stdio functions */
  32. #if __STORM__
  33. #define stccpy(str1,str2,num) strncpy(str1,str2,num)
  34. #endif
  35.  
  36. char name[100],uname[100];
  37.  
  38.  
  39. BOOL ParseILBM(struct IFFHandle *iff)
  40. {
  41.     struct StoredProperty *sp;
  42.     struct BitMapHeader *bmhd;
  43.     UBYTE *body;
  44.     int size,i;
  45.     UBYTE *cols;
  46.     BOOL rc = FALSE;
  47.  
  48.     if (!PropChunk(iff,ID_ILBM,ID_BMHD) &&
  49.         !PropChunk(iff,ID_ILBM,ID_CMAP) &&
  50.         !StopChunk(iff,ID_ILBM,ID_BODY) &&
  51.         !StopOnExit(iff,ID_ILBM,ID_FORM) &&
  52.         !ParseIFF(iff,IFFPARSE_SCAN))
  53.     {
  54.         if (sp=FindProp(iff,ID_ILBM,ID_CMAP))
  55.         {
  56.             cols = (UBYTE *)sp->sp_Data;
  57.  
  58.             printf("#ifdef USE_%s_COLORS\n",uname);
  59.             printf("const ULONG %s_colors[%ld] =\n{\n",name,sp->sp_Size);
  60.             for (i=0;i<sp->sp_Size;i+=3)
  61.                 printf("\t0x%08lx,0x%08lx,0x%08lx,\n",to32(cols[i]),to32(cols[i+1]),to32(cols[i+2]));
  62.             printf("};\n");
  63.             printf("#endif\n\n");
  64.         }
  65.  
  66.         if (sp=FindProp(iff,ID_ILBM,ID_BMHD))
  67.         {
  68.             bmhd = (struct BitMapHeader *)sp->sp_Data;
  69.  
  70.             if ((bmhd->bmh_Compression==cmpNone) || (bmhd->bmh_Compression==cmpByteRun1))
  71.             {
  72.                 size = CurrentChunk(iff)->cn_Size;
  73.  
  74.                 if (body=malloc(size))
  75.                 {
  76.                     if (ReadChunkBytes(iff,body,size)==size)
  77.                     {
  78.                         fprintf(stderr,"Width %d Height %d Depth %d - converting...\n",bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Depth);
  79.  
  80.                         printf("#define %s_WIDTH       %3d\n",uname,bmhd->bmh_Width);
  81.                         printf("#define %s_HEIGHT      %3d\n",uname,bmhd->bmh_Height);
  82.                         printf("#define %s_DEPTH       %3d\n",uname,bmhd->bmh_Depth);
  83.                         printf("#define %s_COMPRESSION %3d\n",uname,bmhd->bmh_Compression);
  84.                         printf("#define %s_MASKING     %3d\n",uname,bmhd->bmh_Masking);
  85.                         printf("\n");
  86.  
  87.                         printf("#ifdef USE_%s_HEADER\n",uname);
  88.                         printf("const struct BitMapHeader %s_header =\n{ %ld,%ld,%ld,%ld,%ld,%ld,%ld,0,%ld,%ld,%ld,%ld,%ld };\n",name,bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Left,bmhd->bmh_Top,bmhd->bmh_Depth,bmhd->bmh_Masking,bmhd->bmh_Compression,bmhd->bmh_Transparent,bmhd->bmh_XAspect,bmhd->bmh_YAspect,bmhd->bmh_PageWidth,bmhd->bmh_PageHeight);
  89.                         printf("#endif\n\n");
  90.  
  91.                         printf("#ifdef USE_%s_BODY\n",uname);
  92.                         printf("const UBYTE %s_body[%ld] = {\n",name,size);
  93.                         for (i=0;i<size;i++)
  94.                         {
  95.                             printf("0x%02lx,",body[i]);
  96.                             if (!((i+1)%15)) printf("\n");
  97.                         }
  98.                         printf(" };\n");
  99.                         printf("#endif\n");
  100.  
  101.                         rc = TRUE;
  102.                     }
  103.                     free(body);
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     return(rc);
  109. }
  110.  
  111.  
  112. int main(int argc,char **argv)
  113. {
  114.     char *c;
  115.     struct IFFHandle *iff;
  116.     struct ContextNode *cn;
  117.  
  118.     if (argc<2 || argc>3 || argv[1][0]=='?')
  119.     {
  120.         printf("Syntax: %s ilbm-file\n",argv[0]);
  121.         exit(20);
  122.     }
  123.  
  124.     stccpy(name,FilePart(argv[1]),100);
  125.     if (c=strchr(name,'.')) *c=0;
  126.     strcpy(uname,name);
  127.     for (c=uname;*c;c++)
  128.         *c=toupper(*c);
  129.  
  130.     if (iff=AllocIFF())
  131.     {
  132.         if (iff->iff_Stream=Open(argv[1],MODE_OLDFILE))
  133.         {
  134.             InitIFFasDOS(iff);
  135.  
  136.             if (!OpenIFF(iff,IFFF_READ))
  137.             {
  138.                 if (!ParseIFF(iff,IFFPARSE_STEP))
  139.                 {
  140.                     if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM))
  141.                     {
  142.                         if (cn->cn_Type==ID_ILBM)
  143.                         {
  144.                             ParseILBM(iff);
  145.                         }
  146.                     }
  147.                 }
  148.                 CloseIFF(iff);
  149.             }
  150.             Close(iff->iff_Stream);
  151.         }
  152.         FreeIFF(iff);
  153.     }
  154.     return(0);
  155. }
  156.